package org.example.aaaatest.a1;

import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);

    public static final FastDateFormat FORMATER = FastDateFormat.getInstance("yyyyMMdd");

    public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    public static final FastDateFormat DATE_FORMAT_HH = FastDateFormat.getInstance("yyyy-MM-dd HH");

    public static final FastDateFormat FORMAT_YMD = FastDateFormat.getInstance("yyyyMMdd");

    public static final FastDateFormat FORMAT_YM = FastDateFormat.getInstance("yyyy-MM");

    public static final FastDateFormat FORMAT_MM = FastDateFormat.getInstance("MM");

    public static final FastDateFormat FORMAT_YM_D2 = FastDateFormat.getInstance("yyyyMM");

    public static final FastDateFormat FORMAT_YM_ZH = FastDateFormat.getInstance("yy年MM月");

    public static final FastDateFormat FORMAT_YMD_ZH = FastDateFormat.getInstance("yy年MM月dd日");

    public static final FastDateFormat FORMAT_YMD_UP = FastDateFormat.getInstance("yyyy-MM-dd");

    public static final FastDateFormat DATE_FORMAT_MIC_SECOND = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSSSSS");

    public static final FastDateFormat FORMAT_YYYYMM_ZH = FastDateFormat.getInstance("yyyy年MM月");

    /**
     * 时分秒
     */
    public static final FastDateFormat DATE_FORMAT_HMS = FastDateFormat.getInstance("yyyyMMddHHmmss");

    public static final DateTimeFormatter COMMON_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter TIME_FORMAT_YYYYMM_ZH = DateTimeFormatter.ofPattern("yyyy年MM月");


    public static int getYesterday() {
        Calendar nowTime = Calendar.getInstance();
        nowTime.set(Calendar.DATE, nowTime.get(Calendar.DATE) - 1);
        String yesterday = FORMATER.format(nowTime);
        return Integer.parseInt(yesterday);
    }

    public static int getWeeksBeforehDate(int date, int weekBefore) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(FORMATER.parse(date + ""));
        calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR) - weekBefore);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
        String lastMonth = FORMATER.format(calendar);
        return Integer.parseInt(lastMonth);
    }

    public static int subtractDay(int n) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.set(Calendar.DATE, nowTime.get(Calendar.DATE) - n);
        return Integer.parseInt(FORMATER.format(nowTime.getTime()));
    }

    public static int dayAfterDay(int date, int n) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(FORMATER.parse(date + ""));
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - n);
        return Integer.parseInt(FORMATER.format(calendar.getTime()));
    }

    /**
     * 获取上个月
     * @return
     */
    public static String getLastMonth() {
        Calendar nowTime = Calendar.getInstance();
        //nowTime.set(Calendar.MONTH, nowTime.get(Calendar.MONTH)-1);修复闰年bug
        nowTime.add(Calendar.MONTH,-1);
        return FORMAT_YM.format(nowTime);
    }

    /**
     * 获取前几个月的月份
     * @param before 前几个月
     * @return 具体月份
     */
    public static String getBeforeMonth(int before) {
        Calendar nowTime = Calendar.getInstance();
        //nowTime.set(Calendar.MONTH, nowTime.get(Calendar.MONTH)-1);修复闰年bug
        nowTime.add(Calendar.MONTH,-before);
        return FORMAT_MM.format(nowTime);
    }

    public static String getBeforeYearMonth(int before) {
        Calendar nowTime = Calendar.getInstance();
        //nowTime.set(Calendar.MONTH, nowTime.get(Calendar.MONTH)-1);修复闰年bug
        nowTime.add(Calendar.MONTH,-before);
        return FORMAT_YYYYMM_ZH.format(nowTime);
    }


    /**
     * 获取D2的日期
     * @return
     */
    public static String getLastMonthD2() {
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MONTH,-1);
        return FORMAT_YM_D2.format(nowTime);
    }

    /**
     *
     * @return
     */
    public static String getLastMonthZh() {
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MONTH,-1);
        return FORMAT_YM_ZH.format(nowTime);
    }

    /**
     * yyyyMM 转为yy年MM月
     * @return
     */
    public static String parseMonthD2ToZh(String monthD2){
        try {
            Date date = DateUtil.FORMAT_YM_D2.parse(monthD2);
            return FORMAT_YM.format(date);
        }catch (Exception e){
            LOGGER.error("parseMonthD2ToZh error,monthD2:{}",monthD2,e);
            return monthD2;
        }
    }

    /**
     * 日期转换String
     * FORMAT_YM_D2
     *
     * @param date
     * @return
     */
    public static String dateToStringTimeD2(Date date) {
        if (date == null) {
            return null;
        }
        try {
            return FORMAT_YM_D2.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 日期转换String
     * FORMAT_YM_D2
     *
     * @param date
     * @return
     */
    public static String dateToStringTimeD3(Date date) {
        if (date == null) {
            return null;
        }
        try {
            return FORMAT_YMD_UP.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取n天后的时间
     *
     * @param n
     * @return yy年MM月dd日
     */
    public static String getDateTimeAfterYearMonDay(int n) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.set(Calendar.DATE, nowTime.get(Calendar.DATE) + n);
        return FORMAT_YMD_ZH.format(nowTime.getTime());
    }

    /**
     * 获取n天前的时间
     * @param n
     * @return yyyyMMdd
     */
    public static String getDateTimeBeforeYearMonDay(int n) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.set(Calendar.DATE, nowTime.get(Calendar.DATE) - n);
        return FORMAT_YMD.format(nowTime.getTime());
    }

    /**
     * 获取n天前的时间
     * @param n
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDateTimeBefore(int n) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.set(Calendar.DATE, nowTime.get(Calendar.DATE) - n);
        return DATE_FORMAT.format(nowTime.getTime());
    }

    /**
     * 获取指定n天前的时间
     * @param n
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDateTimeBefore(Date endDate,int n) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(endDate);
        nowTime.set(Calendar.DATE, nowTime.get(Calendar.DATE) - n);
        return DATE_FORMAT.format(nowTime.getTime());
    }
    /**
     * 日期转成long time
     * @param dateStr
     * @return
     */
    public static long stringToDateTime(String dateStr) {
        if(StringUtils.isBlank(dateStr)){
            return 0;
        }
        try{
            Date date=DateUtil.DATE_FORMAT.parse(dateStr);
            return date.getTime();
        }catch (Exception e){
            return 0;
        }

    }

    /**
     * 日期转换String
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String dateToStringTime(Date date){
        if(date==null){
            return null;
        }
        try{
           return  DATE_FORMAT.format(date);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 日期转换String
     * @param date
     * @return  yy年MM月dd日
     */
    public static String dateToStringTime2(Date date){
        if(date==null){
            return null;
        }
        try{
            return  FORMAT_YMD_ZH.format(date);
        }catch (Exception e){
            return null;
        }
    }

    public static String dateToStringTime3(Date date){
        if (null == date) {
            return null;
        }
        try {
            return DATE_FORMAT_HH.format(date);
        } catch (Exception e){
            return null;
        }
    }

    /**
     * 日期String转换Date
     * @param time
     * @return
     */
    public static Date stringTimeToDate(String time){
        try{
            return DATE_FORMAT.parse(time);
        }catch(Exception e){
            return null;
        }
    }

    /**
     * yyyyMMdd 转为yyyy-MM-dd
     * @return
     */
    public static String parseYmdD2ToUp(String dateStr){
        try {
            Date date = FORMAT_YMD.parse(dateStr);
            return FORMAT_YMD_UP.format(date);
        }catch (Exception e){
            LOGGER.error("parseMonthD2ToZh error,yyyyMMdd:{}",dateStr,e);
            return dateStr;
        }
    }

    /**
     * 获取n个小时后的时间
     *
     * @param date 日期
     * @param n 小时数
     * @return java.util.Date
     * @date 2021/7/8 4:37 PM
     */
    public static Date getLastHourTime(Date date, int n){
        if (null == date) {
            return null;
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.HOUR, n);
        return ca.getTime();
    }

    /**
     * 获取n天后的时间
     * @param n
     * @return yyyyMMdd
     */
    public static Date getLastDateTime(Date date, int n) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        nowTime.set(Calendar.DATE, nowTime.get(Calendar.DATE) + n);
        return nowTime.getTime();
    }

    /**
     * 获取微秒时间格式转换
     * @param str
     * @return
     */
    public static Date getDateByMicSecondFormat(String str){
        if (StringUtils.isBlank(str)) {
            return null;
        }
        try {
            return DATE_FORMAT_MIC_SECOND.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取当前日期 yyyyMMdd
     * @return
     */
    public static String getCurrentDate(){
        Calendar nowTime = Calendar.getInstance();
        return FORMAT_YMD.format(nowTime.getTime());
    }

    /**
     *
     * @param str
     * @return
     */
    public static Date getCurrentDateByStr(String str){
        if (StringUtils.isBlank(str)) {
            return null;
        }
        try {
            return FORMAT_YMD.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     *
     * @param time yyyyMMddHHmmss
     * @return
     */
    public static Date stringTimeToDateSecond(String time){
        try{
            return DATE_FORMAT_HMS.parse(time);
        }catch(Exception e){
            return null;
        }
    }

    public static int getTimeDifferenceToZero() {
        Date nowDate = new Date(); // 当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.set(Calendar.HOUR_OF_DAY, 24);// 设置一天中的第几个小时
        calendar.set(Calendar.MINUTE, 0); // 第几分钟
        calendar.set(Calendar.SECOND, 0); // 第几秒
        Date zero = calendar.getTime(); // 零点
        Long differ = zero.getTime() - nowDate.getTime();
        return differ.intValue() / 1000;
    }

    public static String getCommonTimeStr(long timestamp){
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("+8"));
        return localDateTime.format(COMMON_FORMATTER);
    }


    /**
     * 减去days天
     * @param days
     * @return 毫秒值
     */
    public static long minusDay(int days){
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC+8"));
        // 减去days天
        LocalDateTime daysAgo = now.minusDays(days);
        // 转换为指定时区的ZonedDateTime
        ZonedDateTime zonedDateTime = daysAgo.atZone(ZoneId.systemDefault());
        // 返回时间戳
        return zonedDateTime.toInstant().toEpochMilli();
    }

    /**
     * 获取上个月
     * 如：2025年5月
     */
    public static String getLastMonthWithZh(){
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC+8"));
        LocalDateTime lastMonthTime = now.minusMonths(1L);
        // 返回时间戳
        return lastMonthTime.format(TIME_FORMAT_YYYYMM_ZH);
    }
}
